

#include "..\\Common\\Main.h"
#include "..\\Common\\Vector3.h"
#include "..\\Common\\BitmapFont.h"

using namespace Engine;

App app;
BitmapFont *mBitmapFont;

int DrawGLScene(GLvoid)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
	glLoadIdentity();
	glTranslatef(0.0f,0.0f,-1.0f);						// Move One Unit Into The Screen
	mBitmapFont->SetColor(1.0f,0.0,0.0);
	mBitmapFont->Print(-0.25f,0.12f,"Hello...");
	return true;
}

void InitGL()
{
	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
	mBitmapFont->BuildFont();
	
	//return TRUE;		
}

LRESULT CALLBACK Engine::WndProc(	HWND	hWnd,UINT	uMsg,WPARAM	wParam,	LPARAM	lParam)			
{
	switch (uMsg)									// Check For Windows Messages
	{
		case WM_ACTIVATE:							// Watch For Window Activate Message
		{
			if (!HIWORD(wParam))					// Check Minimization State
			{
				app.mWindow->mActive = TRUE;						// Program Is Active
			}
			else
			{
				app.mWindow->mActive = FALSE;						// Program Is No Longer Active
			}
			return 0;								// Return To The Message Loop
		}

		case WM_SYSCOMMAND:							// Intercept System Commands
		{
			switch (wParam)							// Check System Calls
			{
				case SC_SCREENSAVE:					// Screensaver Trying To Start?
				case SC_MONITORPOWER:				// Monitor Trying To Enter Powersave?
				return 0;							// Prevent From Happening
			}
			break;									// Exit
		}

		case WM_CLOSE:								// Did We Receive A Close Message?
		{
			PostQuitMessage(0);						// Send A Quit Message
			return 0;								// Jump Back
		}

		case WM_KEYDOWN:							// Is A Key Being Held Down?
		{
			app.mKeys[wParam] = TRUE;					// If So, Mark It As TRUE
			return 0;								// Jump Back
		}

		case WM_KEYUP:								// Has A Key Been Released?
		{
			app.mKeys[wParam] = FALSE;					// If So, Mark It As FALSE
			return 0;								// Jump Back
		}

		case WM_SIZE:								// Resize The OpenGL Window
		{
			Engine::ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));  // LoWord=Width, HiWord=Height
			return 0;								// Jump Back
		}
	}

	// Pass All Unhandled Messages To DefWindowProc
	return DefWindowProc(hWnd,uMsg,wParam,lParam);
}




int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	MSG msg;
	BOOL done = FALSE;

	app.Initialize(hInstance);

	

	Engine::CreateGLWindow(hInstance, app);
	mBitmapFont = new BitmapFont(app.mWindow->mHdc);
	ReSizeGLScene(app.mWindow->mWindowRect->right, app.mWindow->mWindowRect->bottom);
	InitGL();

	while(!done)
	{
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
			{
				done  = TRUE;
			}
			else
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}else										// If There Are No Messages
		{
			DrawGLScene();
			// Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
			if (/*(app.mWindow->mActive && !DrawGLScene()) ||*/ app.mKeys[VK_ESCAPE])	// Active?  Was There A Quit Received?
			{
				done=TRUE;							// ESC or DrawGLScene Signalled A Quit
			}
			else									// Not Time To Quit, Update Screen
			{
				SwapBuffers(app.mWindow->mHdc);					// Swap Buffers (Double Buffering)
			}
		}
	}

	//KillGLWindow(hInstance, fullscreen, hRC,hDC,hWnd);
	KillGLWindow(app);
	return msg.wParam;
}